/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.univalle.persistencia;

import co.edu.univalle.modelo.Inmueble;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Jhony
 */
@Stateless
public class InmuebleDAO implements IInmuebleDAO {

    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public void registrar(Inmueble inmueble) {
        try{
            entityManager.persist(inmueble);
        }
        catch(RuntimeException e){
            throw e;
        }  
    }

    @Override
    public void modificar(Inmueble inmueble) {
        try{
            entityManager.merge(inmueble);
        }
        catch(RuntimeException e){
            throw e;
        } 
    }

    @Override
    public void eliminar(Inmueble inmueble) {
        try{
            entityManager.remove(inmueble);
        }
        catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public List<Inmueble> consultar() {
        try{
            String jpql = "SELECT i FROM Inmueble i";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        }
        catch(RuntimeException e){
            throw e;
        }  
    }

    @Override
    public Inmueble consultarPorCodigo(Long codigo) {
        try{
            return entityManager.find(Inmueble.class, codigo);            
        }
        catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public List<Inmueble> consultarPorCliente(Long cedula) {
        try{
            String jpql = "select i from Inmueble i where i.cedula_cliente = "+cedula;
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        }
        catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public List<Inmueble> consultarDisponibles() {
        try{
            String jpql = "select i from Inmueble i where i.disponibilidad = true";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        }
        catch(RuntimeException e){
            throw e;
        }
    }
    
    
}
